//
//  AFNHttpsRequest.m
//  JWT
//
//  Created by Apple on 2017/1/1.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "AFNHttpsRequest.h"
#import "AFNetworking.h"

@implementation AFNHttpsRequest

+(void) requestWithUrl:(id)url WithParam:(id )param WithGetDataDidFinishBlock:(DataDidFinishBlock)didFinishBlock{
    NSString *nurl = nil;
    if([url isKindOfClass:[NSString class]])
    {
        nurl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    if ([url isKindOfClass:[NSURL class]])
    {
        nurl = [url absoluteString];
    }
    if(param) //post
    {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.completionQueue = APPDELEGATE.afnCompletionQueue;
        
        //自定义header
//        if (APPDELEGATE.isLogin == NO) {
//            NSString *cookie = Tanant;
//            [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
//        }else{
//            //上传cookie
//            NSHTTPCookie *cookie1 = [APPDELEGATE.cookiearr objectAtIndex:0];
//            NSHTTPCookie *cookie2 = [APPDELEGATE.cookiearr objectAtIndex:1];
//            NSString *cookie = [NSString stringWithFormat:@"%@=%@;%@=%@",cookie1.name,cookie1.value,cookie2.name,cookie2.value];
//            [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
//        }
        
//        [manager.requestSerializer setValue:@"text/html; charset=utf-8" forHTTPHeaderField:@"Content-Type" ];
        [manager POST:nurl parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"responseObject--%@",NSJSONStringWithObject(responseObject));
            if(didFinishBlock)
            {
                didFinishBlock(responseObject,task,nil);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (task.response == nil) {
                [HUD showToast:NET_CONNECT_TIMEOUT];
            }
            if(didFinishBlock)
            {
                didFinishBlock(nil,task,error);
            }
        }];
    }
    else
    {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.completionQueue = APPDELEGATE.afnCompletionQueue;
        
        //自定义header
//        if (APPDELEGATE.isLogin == NO) {
//            NSString *cookie = Tanant;
//            [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
//        }else{
//            //上传cookie
//            NSHTTPCookie *cookie1 = [APPDELEGATE.cookiearr objectAtIndex:0];
//            NSHTTPCookie *cookie2 = [APPDELEGATE.cookiearr objectAtIndex:1];
//            NSString *cookie = [NSString stringWithFormat:@"%@=%@;%@=%@",cookie1.name,cookie1.value,cookie2.name,cookie2.value];
//            [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
//        }
        
        [manager GET:nurl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"responseObject--%@",NSJSONStringWithObject(responseObject));
            if(didFinishBlock)
            {
                didFinishBlock(responseObject,task,nil);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (task.response == nil) {
                [HUD showToast:NET_CONNECT_TIMEOUT];
            }
            if(didFinishBlock)
            {
                NSLog(@"%@",error);
                didFinishBlock(nil,task,error);
            }
        }];
    }
};


@end
